package com.infore.siteManagement.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.dto.ItemAndStationDto;
import com.infore.siteManagement.entity.Item;
import com.infore.siteManagement.entity.ItemAndStation;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.IitemAndStationRepository;
import com.infore.siteManagement.repository.IitemRepository;
import com.infore.siteManagement.service.IitemService;
import com.infore.siteManagement.util.ArrayUtils;
import com.infore.siteManagement.util.ItemTreeUtil;
import com.infore.siteManagement.util.ResultEntity;
import com.infore.siteManagement.vo.ItemStationVo;
import com.infore.siteManagement.vo.ItemTreeVo;

@Service
public class ItemServiceImpl implements IitemService {

	@Autowired
	private IitemRepository itemRepository;
	@Autowired
	private IitemAndStationRepository itemAndStationRepository;

	@Override
	public ResultEntity query(String name) {
		List<Item> list = null;
		if (StringUtils.isNotBlank(name)) {
			list = itemRepository.findByNameLikeOrderBySortAsc("%" + name + "%");
		} else {
			list = itemRepository.findAll();
		}
		List<ItemTreeVo> rootList = new ArrayList<ItemTreeVo>();
		List<ItemTreeVo> bodyList = new ArrayList<ItemTreeVo>();
		for (Item item : list) {
			String id = item.getId();
			String nodeName = item.getName();
			String pId = item.getParientId();
			Integer sort = item.getSort();
			ItemTreeVo vo = new ItemTreeVo();
			vo.setNodeId(id);
			vo.setNodeName(nodeName);
			vo.setPid(pId);
			vo.setSort(sort);
			vo.setLevel(item.getItemLevel());
			if (StringUtils.isBlank(pId)) {
				rootList.add(vo);
			} else {
				bodyList.add(vo);
			}
		}
		ItemTreeUtil util = new ItemTreeUtil(rootList, bodyList);
		List<ItemTreeVo> itemTreeVos = util.getTree();
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(itemTreeVos);
	}

	@Override
	public ResultEntity delete(String ids) {
		try {
			List<String> idsList = ArrayUtils.stringToList(ids);
			itemRepository.deleteByIdIn(idsList);
			return new ResultEntity(ResultStatus.SUCCESS, Constants.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ResultEntity(ResultStatus.SUCCESS, Constants.DELETE_FAILUE);
	}

	@Override
	public ResultEntity edit(Item item) {
		List<Item> list = itemRepository.findByNameOrderBySortAsc(item.getName());
		if (list.size() > 0 && StringUtils.isBlank(item.getId())) {
			return new ResultEntity(ResultStatus.ERROR, "项目名称已存在");
		}
		itemRepository.saveAndFlush(item);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS);
	}

	@Override
	public ResultEntity level(String level) {
		List<Item> items = itemRepository.findByItemLevelOrderBySortAsc(level);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.DEAL_SUCCESS).setDataList(items);
	}

	@Override
	public ResultEntity itemAndStation(ItemAndStationDto itemAndStationDto) {
		List<String> ids = ArrayUtils.stringToList(itemAndStationDto.getItemId());
		List<ItemAndStation> list = new ArrayList<ItemAndStation>();
		List<String> stationIds = itemAndStationDto.getStationIds();
		String itemId = itemAndStationDto.getItemId();
		for (String string : stationIds) {
			ItemAndStation itemAndStation = new ItemAndStation();
			itemAndStation.setItemId(itemId);
			itemAndStation.setStationId(string);
			list.add(itemAndStation);
		}
		itemAndStationRepository.deleteByItemIdIn(ids);
		itemAndStationRepository.flush();
		itemAndStationRepository.saveAll(list);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.INSERT_SUCCESS);
	}

	@Override
	public ResultEntity queryStation(String itemId) {
		List<ItemStationVo> list = itemRepository.queryByItemId(itemId);
		return new ResultEntity(ResultStatus.SUCCESS, Constants.QUERY_SUCCESS).setDataList(list);
	}

}
